package com.jkoss.action.kc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.protocol.HTTP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jkoss.biz.kc.HomeworkBiz;
import com.jkoss.biz.kc.subbiz;
import com.jkoss.pojo.kc.Homework;
import com.jkoss.pojo.kc.Subject;
import com.jkoss.pojo.oa.Emps;
import com.jkoss.tool.Page;

@Controller
@RequestMapping("/kc")
public class HomeworkAction {
    @Autowired
	private HomeworkBiz hwbiz;
    
    @Autowired
    private subbiz subiz;
	
    //查询全部
    @RequestMapping("listHw")
    public String listPageHomework(HttpServletRequest req,Page<Homework> page,Integer sbid){
		
    	if(page==null){
			page=new Page<Homework>();
		}
    	/*if(sbid==null||sbid==0){
    	page.setResults(hwbiz.listPageHw(page,sbid));
    	req.setAttribute("page", page);
    	}else{*/
    		page.setResults(hwbiz.listPageHw(page,sbid));
    		req.setAttribute("sbid", sbid);
        	req.setAttribute("page", page);
    	/*}*/
    	//查询科目   
    	List<Subject> subs=subiz.findsubByAll2();
    	
    	/*List<Subject> subs=subiz.findsubs();*/
    	req.setAttribute("subs", subs);
    	return "listHw.jsp";
    	
    }
	
    //查询单个
    @RequestMapping("oneHomework")
    public String OneHomework(int hkid,HttpServletRequest req){
		Homework hw=hwbiz.findById(hkid);
    	if(hw!=null){
    		req.setAttribute("hw", hw);
    	}
    	return "shHw.jsp";	
    }
    //删除
    @RequestMapping("delHw")
    public String delHomework(int hkid,HttpServletRequest req){
		String msg=hwbiz.delHw(hkid);
    	
		req.setAttribute("msg", msg);
		return listPageHomework(req,null,null);
    	
	
    	
    }
    
    //添加
    @RequestMapping("addHw")
    public String addHomework(Homework homework,HttpServletRequest req){
		Emps emp=(Emps) req.getSession().getAttribute("lgnUsr");
		homework.setEid(emp.getEid());
		String msg=hwbiz.addHw(homework);
		req.setAttribute("msg", msg);
    	return listPageHomework(req,null,null);	
    }
    
    @RequestMapping(value="/ajOneHw.do",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String toUpdHomework(int  hkid){
		
		return   JSONObject.toJSONString(hwbiz.findById(hkid)).toString();
	}
    
    //修改
    @RequestMapping("updHw")
    public String updHomework(Homework homework,HttpServletRequest req){
		
    	Homework hw=hwbiz.findById(homework.getHkid());
    	hw.setSbname(homework.getSbname());
    	hw.setHktitle(homework.getHktitle());
    	hw.setHkans(homework.getHkans());
    	hw.setHkdescb(homework.getHkdescb());
    	hw.setSbid(homework.getSbid());
    	
    	String msg=hwbiz.updHw(hw);
    	req.setAttribute("msg", msg);
    	return listPageHomework(req,null,null);
    }
    
   /* @RequestMapping("lisSubs")
    public String listSubs(int sbid,HttpServletRequest req,Page<Homework> page){
    	
    	
    	
    	System.out.println(sbid);
    	if(sbid==0){
    		return listPageHomework(req,page);
    	}else{
    	
    	if(page==null){
			page=new Page<Homework>();
		}
    	
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("sbid", sbid);
    	page.setParams(params);
		page.setResults(hwbiz.findHwBySbid(page));
    	req.setAttribute("page", page);
    	
    	List<Subject> subs=subiz.findsubs();
    	req.setAttribute("subs", subs);
    	return "listHw.jsp";
    	}
    }
    */
}
